package november.stack;

/**
 * @author huangxin
 * @date 2019/11/15-15:16
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack linkedListStack = new LinkedListStack();
        linkedListStack.push( new Node(1,""));
        linkedListStack.push(new Node(2,""));
        linkedListStack.push(new Node(3,""));

        System.out.println("linkedListStack.size() = " + linkedListStack.size());

        while (linkedListStack.size() > 0) {
            System.out.println(linkedListStack.pop());
        }
    }
}

class LinkedListStack implements Stack<Node> {
    private Node head = new Node(0, "");

    @Override
    public void push(Node value) {
        value.next = head.next;
        head.next = value;
    }

    @Override
    public Node pop() {
        if (isEmpty()) {
            System.out.println("链表为空");
            return null;
        }
        Node result = head.next;
        head.next = head.next.next;
        return result;
    }

    @Override
    public void show() {
        if (isEmpty()) {
            System.out.println("链表为空");
            return;
        }

        Node temp = head.next;

        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }

    }

    @Override
    public int size() {
        int count = 0;
        Node temp = head.next;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    @Override
    public boolean isEmpty() {
        return head.next == null;
    }
}

class Node {
    int no;
    String name;
    Node next;

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
